package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/sp"
)

// 8550 获取活动礼包道具
func (p *Player) ActPackReqRequest(req *sp.ActPackReqRequest) (res *sp.ActPackReqResponse) {
	us := p.User
	// 玩家没有到达1-2关，没有每日礼包道具
	if us.MaxPos.LockBlockTid < 2 {
		res = &sp.ActPackReqResponse{}
		return
	}
	ap, err := lib.GetActPack(us)
	if err != nil {
		res = &sp.ActPackReqResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	bp, _ := lib.GetBossPack(us)

	res = &sp.ActPackReqResponse{
		Lst:        lib.FormatGiftPacktSps(ap),
		BossPayLst: lib.FormatBossPacktSps(bp),
	}
	return
}

// 获取指定礼包信息
func (p *Player) ActPackReqIdRequest(req *sp.ActPackReqIdRequest) (res *sp.ActPackReqIdResponse) {
	gp, err := lib.GetActPackId(p.User, req.Tid)
	if err != nil {
		res = &sp.ActPackReqIdResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	res = &sp.ActPackReqIdResponse{
		GiftPackInfo: lib.FormatGiftPacktSp(gp),
	}
	return
}

// 8552 不扣钱，购买活动礼包
func (p *Player) ActPackBuyRequest(req *sp.ActPackBuyRequest) (res *sp.ActPackBuyResponse) {
	buyingPackId, ap, uProp, resGoods, boxResList, err := lib.BuyActPack("ActPackBuyRequest", p.User, req.Tid, p.UnionSendRedPacketReq, p.Send)
	if err != nil {
		res = &sp.ActPackBuyResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	buyingPackConfig, err := lib.GetPaidPropsPacksConfig(buyingPackId)
	if err == nil {
		// 首充状态设置
		if p.User.FirstReChargeStatus == 0 && int64(buyingPackConfig.Typ) != data.PaidPropsEnum.FreePack {
			p.User.SetFirstRechargeStatus(1)
		}
	}
	//fmt.Printf("购买礼包 %d,FirstStatus %d.packType %d \n", req.Tid, p.User.FirstReChargeStatus, buyingPackConfig.Typ)
	res = &sp.ActPackBuyResponse{
		Errcode:             0,
		Lst:                 lib.FormatGiftPacktSps(ap),
		Res:                 lib.FormatGoodsInfoSps(resGoods),
		BoxRes:              lib.FormatBoxResSps(boxResList),
		FirstReChargeStatus: p.User.FirstReChargeStatus,
	}
	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	return
}

// 8560 购买免费礼包
func (p *Player) FreePackBuyRequest(req *sp.FreePackBuyRequest) (res *sp.FreePackBuyResponse) {
	if req.Tid == 900 { // 首充奖励包
		if p.User.FirstReChargeStatus != 1 { // 首充奖励状态 0-未达成 1-达成未领取 2-达成并领取
			err := errors.NewErrcode(data.Errcode.ServerErr, "不能重复领取首充礼包")
			res = &sp.FreePackBuyResponse{
				Errcode: errors.ErrCode(err),
			}
			return
		}
	}
	// 领取礼包
	uProp, resGoods, boxResList, err := lib.BuyFreePack(p.User, req.Tid, "", p.Send)
	if err != nil {
		res = &sp.FreePackBuyResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	if req.Tid == 900 { // 首充奖励包
		if p.User.FirstReChargeStatus == 1 { // 首充奖励状态 0-未达成 1-达成未领取 2-达成并领取
			p.User.SetFirstRechargeStatus(2)
		}
	}
	// 免费包，就不用设置首充状态了，用回原来的状态
	res = &sp.FreePackBuyResponse{
		Errcode:             0,
		Res:                 lib.FormatGoodsInfoSps(resGoods),
		BoxRes:              lib.FormatBoxResSps(boxResList),
		FirstReChargeStatus: p.User.FirstReChargeStatus,
	}
	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	return
}
